﻿@using CubicFWNet.Data.ViewModal
@model CubicFWNet.Data.Models.PurchaseCompose
@*@model IEnumerable<ProductTechnologyView>*@
@{
    //ViewBag.Title = "价格核算";
    var operationName = ViewBag.OperationName;
    var operationTime = ViewBag.OperationTime;
    //var ord = ViewBag.Order as CubicFWNet.Data.Models.Order;
    var pro = ViewBag.Product as CubicFWNet.Data.Models.Product;
    var customerList = ViewBag.CustomerList as IEnumerable<CubicFWNet.Data.Models.Customer>;
    var pvList = ViewBag.PvList as List<ProductTechnologyView>;
    //var userName = ViewBag.UserName;
    //var userId = ViewBag.UserId;
    var purchaseNum = ViewBag.PurchaseNum;
    var earliestTime = ViewBag.EarliestTime;
    var earliestPrice = ViewBag.EarliestPrice;

    var checkInfoMemo = ViewBag.CheckInfoMemo;
}
<section class="content-header">
    <h1>
        采购单
    </h1>
    <ol class="breadcrumb">
        <li><a href="javascript:void(0);"><i class="fa fa-dashboard"></i>@Resources.Global.PageTitle</a></li>
        <li><a href="@Url.Action("Index", "Home")">首页</a></li>
        <li class="active"><a href="@Url.Action("PurQuery", "Purchase")">采购查询</a></li>
    </ol>
</section>
<section class="content">
    <div class="box box-solid">
        <!-- /.box-header -->
        <div class="box-body">
            <div class="row">
                @if (Model != null && pro != null)
                {
                    <form class="form-horizontal" id="frmedit" action="@Url.Action("UpdatePurchase", "Purchase")" method="post">
                        <div class="box-body">
                            <input type="hidden" id="Id" name="Id" value="@Model.Id" />
                            <input type="hidden" id="AmountReal" name="AmountReal" value="@Model.Amount" />
                            @*<input type="hidden" id="ApplyId" name="ApplyId" value="@userId" />*@
                            <input type="hidden" id="ApplyTime" name="ApplyTime" value="@Model.ApplyTime" />
                            <input type="hidden" id="ProductId" name="ProductId" value="@Model.ProductId" />
                            <input type="hidden" id="Type" name="Type" value="@Model.Type" />
                            @*<div class="form-group col-md-6">
                                    <label class="col-sm-3 control-label">申请人</label>
                                    <div class="col-sm-9">
                                        <input type="text" class="form-control" id="ApplyName" name="ApplyName" value="@userName" readonly="readonly" />
                                    </div>
                                </div>*@
                            <div id="proTableListBox">
                                @if (pvList != null)
                                {
                                    foreach (var item in pvList)
                                    {
                                        <div class="col-md-12" style="border-bottom: 1px solid #ccc; margin-top: 8px;">
                                            <div class="form-group col-md-4">
                                                <label class="col-sm-5 control-label">订单号</label>
                                                <div class="col-sm-7" style="padding-top: 7px; margin-bottom: 0;">
                                                    <a href="javascript:void(0)" onclick="ShowModal('@item.OrderId')">@item.OrderNum</a>
                                                </div>
                                            </div>
                                            <div class="form-group col-md-4">
                                                <label class="col-sm-5 control-label">工艺</label>
                                                <div class="col-sm-7" style="padding-top: 7px; margin-bottom: 0;">
                                                    <a href="javascript:void(0)" class="technologyinfo" data-id="@item.TechnologyId">查看工艺</a>
                                                </div>
                                            </div>
                                            <div class="form-group col-md-4">
                                                <label class="col-sm-5 control-label">数量</label>
                                                <div class="col-sm-5" style="padding-top: 7px; margin-bottom: 0;">
                                                    <span>@item.Amount</span>
                                                </div>
                                                <div class="col-sm-2" style="padding-top: 7px; margin-bottom: 0;">
                                                    <input type="checkbox" class="minimal pull-right minimalBox" checked="checked" data-num="@item.Amount" data-orderid="@item.PurchaseId">
                                                </div>
                                            </div>
                                        </div>
                                        <div class="form-group col-md-12" style="height: 9px;"></div>
                                    }
                                    <div class="form-group col-md-12" style="height: 1px;"></div>
                                }
                            </div>

                            <div class="form-group col-md-6">
                                <label class="col-sm-3 control-label">产品名称</label>
                                <div class="col-sm-9">
                                    <input type="text" class="form-control" id="ProductName" name="ProductName" value="@pro.Name" readonly="readonly" />
                                </div>
                            </div>
                            <div class="form-group col-md-6">
                                <label class="col-sm-3 control-label">采购单号</label>
                                <div class="col-sm-9">
                                    <input type="text" class="form-control" id="Number" name="Number" value="@purchaseNum" readonly="readonly" />
                                </div>
                            </div>
                            @*<div class="form-group col-md-6">
                                    <label class="col-sm-3 control-label">供应商</label>
                                    <div class="col-sm-9">
                                        <input type="hidden" name="SupplierId1" id="SupplierId1" />
                                        <select class="form-control" type="select" id="SupplierId" name="SupplierId">
                                            <option value="0" selected="selected">==请选择==</option>
                                            @if (customerList != null)
                                            {
                                                foreach (var item in customerList)
                                                {
                                                    <option value="@item.Id">@item.Name</option>
                                                }
                                            }
                                        </select>
                                    </div>
                                </div>*@


                            <div class="form-group col-md-6">
                                <label class="col-sm-3 control-label">供应商</label>
                                <div class="col-sm-9">
                                    <input type="hidden" name="SupplierId1" id="SupplierId1" />
                                    <input type="hidden" name="SupplierId" id="SupplierId" />
                                    <span class="btn btn-primary" onclick="ShowCusList()">选择供应商</span>&nbsp;&nbsp;&nbsp;&nbsp;
                                    <span id="CustomerName"></span>
                                </div>
                            </div>


                            <div class="form-group col-md-6">
                                <label class="col-sm-3 control-label">采购数量</label>
                                <div class="col-sm-9">
                                    <input type="text" class="form-control okeydown" id="Amount" name="Amount" value="@Model.Amount" readonly="readonly" />
                                </div>
                            </div>
                            <div class="form-group col-md-6">
                                <label class="col-sm-3 control-label">采购价(单价)</label>
                                <div class="col-sm-9">
                                    <input type="text" class="form-control okeydown" id="CostPrice" name="CostPrice" value="@earliestPrice" required />
                                </div>
                            </div>
                            <div class="form-group col-md-6" style="display: none">
                                <label class="col-sm-3 control-label">实际价格</label>
                                <div class="col-sm-9">
                                    <input type="text" class="form-control okeydown" id="ActualPrice" name="ActualPrice" value="@pro.Price" />
                                </div>
                            </div>

                            <div class="form-group col-md-12" style="height: 1px;"></div>
                            <div class="form-group col-md-6">
                                <label class="col-sm-3 control-label">宽度-mm</label>
                                <div class="col-sm-9">
                                    <input type="text" class="form-control" id="SizeWidth" name="SizeWidth" value="@pro.SizeWidth" readonly="readonly">
                                </div>
                            </div>
                            <div class="form-group col-md-6">
                                <label class="col-sm-3 control-label">长度-mm</label>
                                <div class="col-sm-9">
                                    <input type="text" class="form-control" id="SizeLenth" name="SizeLenth" value="@pro.SizeLenth" readonly="readonly" />
                                </div>
                            </div>
                            <div class="form-group col-md-6">
                                <label class="col-sm-3 control-label">基本单位</label>
                                <div class="col-sm-9">
                                    <input type="text" class="form-control" id="Unit" name="Unit" value="@pro.Unit" readonly="readonly">
                                </div>
                            </div>
                            <div class="form-group col-md-6">
                                <label class="col-sm-3 control-label">包装单位</label>
                                <div class="col-sm-9">
                                    <input type="text" class="form-control" id="Pack" name="Pack" value="@pro.Pack" readonly="readonly">
                                </div>
                            </div>
                            <div class="form-group col-md-12" style="height: 1px;"></div>
                            <div id="json-content">
                                <div class="form-group col-md-6">
                                    <label class="col-sm-3 control-label">希望交货时间</label>
                                    <div class="col-sm-9">
                                        <input type="text" class="form-control Timedatepicker" data-des="希望交货时间" id="j_a" name="j_a" value="@earliestTime" required />
                                    </div>
                                </div>
                                <div class="form-group col-md-6">
                                    <label class="col-sm-3 control-label">执行人</label>
                                    <div class="col-sm-9">
                                        <input type="text" class="form-control" data-des="执行人" id="j_b" name="j_b" value="@operationName" required />
                                    </div>
                                </div>
                                <div class="form-group col-md-6">
                                    <label class="col-sm-3 control-label">订单执行日</label>
                                    <div class="col-sm-9">
                                        <input type="text" class="form-control Timedatepicker" data-des="订单执行日" id="j_c" name="j_c" value="" required />
                                    </div>
                                </div>
                                <div class="form-group col-md-6">
                                    <label class="col-sm-3 control-label">预计到货时间</label>
                                    <div class="col-sm-9">
                                        <input type="text" class="form-control Timedatepicker" data-des="预计到货时间" id="j_d" name="j_d" value="" required />
                                    </div>
                                </div>
                                @*<div class="form-group col-md-6">
                                        <label class="col-sm-3 control-label">到货数量</label>
                                        <div class="col-sm-9">
                                            <input type="text" class="form-control" id="ArrivalAmount" data-des="到货数量" name="ArrivalAmount" value=""/>
                                        </div>
                                    </div>*@
                                <div class="form-group col-md-6">
                                    <label class="col-sm-3 control-label">到货日期</label>
                                    <div class="col-sm-9">
                                        <input type="text" class="form-control Timedatepicker" id="ArrivalTime" data-des="到货日期" name="ArrivalTime" value="" />
                                    </div>
                                </div>
                                @*<div class="form-group col-md-6">
                                        <label class="col-sm-3 control-label">订单执行完毕</label>
                                        <div class="col-sm-9">
                                            <input type="text" class="form-control" data-des="订单执行完毕" id="j_e" name="j_e" value="" required />
                                        </div>
                                    </div>*@
                            </div>
                            <div class="form-group col-md-12" style="height: 1px;"></div>
                            <div class="form-group col-md-6">
                                <label class="col-sm-3 control-label">备注</label>
                                <div class="col-sm-9">
                                    <textarea class="form-control" rows="3" placeholder="请输入 ..." name="Memo" id="Memo"></textarea>
                                </div>
                            </div>
                            <div class="form-group col-md-6">
                                <label class="col-sm-3 control-label">成本核算备注</label>
                                <div class="col-sm-9">
                                    <textarea class="form-control" rows="3" readonly="readonly">@checkInfoMemo</textarea>
                                </div>
                            </div>
                            <div class="form-group col-md-12" style="height: 1px;"></div>
                            <div class="form-group col-md-6">
                                <label for="operationName" class="col-sm-3 control-label">操作人</label>
                                <div class="col-sm-9">
                                    <input type="text" class="form-control" id="operationName" name="operationName" value="@operationName" readonly="readonly">
                                </div>
                            </div>
                            <div class="form-group col-md-6">
                                <label for="operationTime" class="col-sm-3 control-label">日期</label>
                                <div class="col-sm-9">
                                    <input type="text" class="form-control" id="operationTime" name="operationTime" value="@operationTime" readonly="readonly">
                                </div>
                            </div>
                        </div>
                        <div class="box-footer" style="width: 80%; margin-left: 20px;">
                            <a role="button" href="javascript:void(0)" class="btn btn-primary submitbtn" onclick="formsubmit(1)">提交</a>
                            <a role="button" href="@Url.Action("PurQuery", "Purchase")" clas class=" btn btn-primary submitbtn">返回</a>
                            <div class="text-red" style="margin-top:20px;font-weight:bold">下一步：采购审核-->采购查询</div>
                        </div>
                    </form>
                }
            </div>
        </div>
    </div>
</section>

<!--选择供应商-->
<div class="modal" id="cusList" style="display: none">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title">选择供应商</h4>
            </div>
            <div class="modal-body">
                <div class="row">
                    <div class="form-group col-sm-8">
                        <label for="GetCus" class="col-sm-3 " >搜索</label>
                        <div class="col-sm-9">
                            <input type="text" class="form-control" id="GetCus" name="GetCus" placeholder="请输入供应商名称..." onkeyup="GetCus()" />
                        </div>
                    </div>
                </div>
                <div id="CusList">
                    @foreach (var item in customerList)
                    {
                        if (item.Id== Model.SupplierId)
                        {
                            <span class="btn btn-primary customerItem" data-cusID="@item.Id" data-cusName="@item.Name" data-sel="1" style="margin:8px" data-dismiss="modal">@item.Name</span>
                        }
                        else
                        {
                            <span class="btn btn-default customerItem" data-cusID="@item.Id" data-cusName="@item.Name" data-sel="0" style="margin:8px" data-dismiss="modal">@item.Name</span>
                        }
                    }
                </div>
            </div>
            <div class="modal-footer">
                <a role="button" class="btn btn-default pull-left" data-dismiss="modal">关闭</a>
                <a role="button" class="btn btn-primary" data-dismiss="modal">完成</a>
            </div>
        </div>
    </div>
</div>



<div id="loadingPage" style="display: none;">
    <div class="loading-shade"></div>
    <div class="loading-content" onclick="$.loading(false)">数据加载中，请稍后…</div>
</div>

<style>
    .has-error {
        border-color: #dd4b39;
        box-shadow: none;
    }
</style>


@section Scripts{
    <script type="text/javascript">
        var jsonData = { WebCheckbox: {} };
        jsonData.WebCheckbox.Ids = [];

        $(document).ready(function () {
            //日期控件
            $(".Timedatepicker").datepicker({
                autoclose: true,
                language: 'zh', //汉化
                format: "yyyy-mm-dd", //选择日期后，文本框显示的日期格式
                minView: "month"//选择日期后，不会再跳转去选择时分秒
            });

            $("#frmedit").validate({
                submitHandler: function (form) {
                    var postData = $(form).formSerialize();
                    postData.Data = getParam();
                    postData.guids = jsonData.WebCheckbox.Ids;
                    $.submitForm({
                        url: $(form).attr("action"),
                        param: postData,
                        success: function () {
                            $.modalAlert("操作成功", "success");
                            window.location.href = '@Url.Action("PurQuery", "Purchase")';
                        }
                    });
                }
            });
        });

        $('input[type="checkbox"].minimal,input[type="radio"].minimal').iCheck({
            checkboxClass: 'icheckbox_minimal-blue',
            radioClass: 'iradio_minimal-blue'
        });
        //首次加载页面
        $("#proTableListBox").find("input[type='checkbox'].minimal").each(function (i, e) {
            if (jsonData.WebCheckbox.Ids.indexOf($(e).attr("data-orderId")) > -1) {
                //包含
                //$(e).iCheck('check');
            } else {
                //不包涵
                //$(e).iCheck('uncheck');
                jsonData.WebCheckbox.Ids.push($(e).attr("data-orderId")); //添加对应的Id'
            }
        });
        console.log(jsonData.WebCheckbox.Ids);

        function formsubmit(t) {
            var value = $("#SupplierId").val();
            if (value == "" || value == "0") {
                $.modalAlert("请选择供应商", "warning");
                return false;
            }
            var totalNum = $("#Amount").val();
            if (totalNum == "" || isNaN(totalNum) || totalNum == "Infinity") {
                $.modalAlert("采购数量错误，请尝试刷新页面", "warning");
                return false;
            }
            if (totalNum == "0") {
                $.modalAlert("采购数量不可为0", "warning");
                return false;
            }

            $("#frmedit").submit();
        }

        //多选选中
        $('input[type="checkbox"].minimalBox').on('ifClicked', function (event) {
            var totalNum = $("#Amount").val() / 1;
            var orderId = $(this).attr("data-orderid");
            var num = $(this).attr("data-num") / 1;

            //现在判断时若是选中的，点击完了就变成了未选中了
            if ($(this).is(":checked")) {

                jsonData.WebCheckbox.Ids.splice($.inArray(orderId, jsonData.WebCheckbox.Ids), 1); //删除对应的Id'
                $(this).removeClass("checkboxitem");
                var subNum = totalNum - num;
                if (subNum < 0) {
                    subNum = 0;
                }
                $("#Amount").val(subNum);
            } else //这里判断是没选中，点击完了就变成选中了
            {
                if (jsonData.WebCheckbox.Ids.indexOf(orderId) <= -1) {
                    $(this).addClass("checkboxitem");
                    jsonData.WebCheckbox.Ids.push(orderId); //添加对应的Id'
                    var addNum = totalNum + num;
                    $("#Amount").val(addNum);
                } else {
                    $.modalAlert("采购Id重复，请刷新", "warning");
                    return false;
                }
            }
            console.log(jsonData.WebCheckbox.Ids);
        });



        $(".okeydown").on("keyup", function () {
            var value = $(this).val();
            if (isNaN(value) || value == "" || value == "Infinity" || value == "NaN") {
                $(this).addClass("has-error");
            } else {
                $(this).removeClass("has-error");
            }
        });


        $(".okeydown").on("keydown", function () {
            var k = event.keyCode;
            if ((k <= 57 && k >= 48) || (k <= 105 && k >= 96) || (k == 8) || (k == 110) || (k == 190)) {
                return true;
            } else {
                return false;
            }
        });

        //json-content
        function getParam() {
            var postdata = [];
            //主键
            $("#json-content").find('input,select,textarea').each(function (r) {
                var $this = $(this);
                var _des = $this.attr('data-des');
                var _id = $this.attr('id');
                var type = $this.attr('type');
                switch (type) {
                    case "checkbox":
                        postdata.push({ des: _des, name: _id, value: $this.is(":checked") });
                        break;
                    case "select":
                        var value = $this.find("option:selected").val() == "" ? null : $this.find("option:selected").val();
                        postdata.push({ des: _des, name: _id, value: value });
                        break;
                    default:
                        var value = $this.val() == "" ? "&nbsp;" : $this.val();
                        postdata.push({ des: _des, name: _id, value: value });
                        break;
                }

            });
            return JSON.stringify(postdata);
        }

        $(".technologyinfo").click(function () {
            var id = $(this).data('id');
            var options = {
                id: null,
                title: '产品信息',
                width: "100%",
                height: "100%",
                url: "../Technology/TechnologyInfo?tId=" + id,
                shade: 0.3,
                btn: ['返回'],
                btnclass: ['btn btn-danger'],
                callBack: null
            }
            $.modalOpen(options);
        });
        //订单信息弹窗
        function ShowModal(id) {
            var options = {
                id: null,
                title: '订单信息',
                width: "100%",
                height: "100%",
                url: "../Order/OrderInfo?OID=" + id,
                shade: 0.3,
                btn: ['返回'],
                btnclass: ['btn btn-danger'],
                callBack: null
            }
            $.modalOpen(options);
        }


        //弹窗显示供应商
        function ShowCusList() {
             $("#cusList").modal();
        }

        //点击选择供应商
        $(".customerItem").click(function () {
            var id = $(this).attr("data-cusID");
            var name = $(this).attr("data-cusName");
            $("#CustomerName").html(name);
            $("#SupplierId").val(id);
            
            $(".customerItem").addClass("btn-default");
            $(".customerItem").removeClass("btn-primary");
            $(this).addClass("btn-primary");
            $(this).removeClass("btn-default");
        });

        //搜索供应商
        function GetCus() {
            var name = $("#GetCus").val().trim();

            var cusList = $("#CusList .customerItem");
            var num = cusList.size();

            for (var i = 0; i < num; i++) {
                var cusName = $("#CusList .customerItem").eq(i).attr("data-cusName");
                if (cusName.indexOf(name) >= 0) {
                    $("#CusList .customerItem").eq(i).show();
                }
                else {
                    $("#CusList .customerItem").eq(i).hide();
                }
            }
        }

    </script>
}